package com.skinchange.skin;

import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;

import java.lang.reflect.Method;

/**
 * Description:
 * Author: Author
 * Date: 2017-08-08
 * Time: 16:52
 */


public class SkinManager {

    private Context mContext;

    private static SkinManager sInstance;
    private ResourcesManager mResourcesManager;

    private SkinManager() {

    }

    public static SkinManager getInstance(){
        if (sInstance == null){
            synchronized (SkinManager.class){
                if (sInstance == null){
                    sInstance = new SkinManager();
                }
            }
        }
        return sInstance;
    }

    public void init(Context context){
        mContext = context.getApplicationContext();
    }


    public ResourcesManager getResourcesManager(){
        return mResourcesManager;
    }


    private void loadPlugin(String skinPluginPath, String skinPluginPkg) {

        try {
            AssetManager assetManager = AssetManager.class.newInstance();
            Method addAssetPathMethod = assetManager.getClass().getMethod("addAssetPath",String.class);
            addAssetPathMethod.invoke(assetManager,skinPluginPath);

            Resources superResources = mContext.getResources();
            Resources resources = new Resources(assetManager,superResources.getDisplayMetrics(),superResources.getConfiguration());

            mResourcesManager = new ResourcesManager(resources,skinPluginPkg);



        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
